//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.come.control;

import com.tool.image.ImageMixDeal;
import com.tool.tcp.NewPart;
import com.tool.tcp.SpriteFactory;
import java.math.BigDecimal;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.ImageDressJframe;
import org.come.Frame.RankingListJframe;
import org.come.Jpanel.RankingListJpanel;
import org.come.action.FromServerAction;
import org.come.bean.LoginResult;
import org.come.bean.UserRoleArrBean;
import org.come.tt.LadderJframe;
import org.come.tt.LadderJpanel;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;

public class RankingListControl implements FromServerAction {
	public RankingListControl() {
	}

	public void controlMessFromServer(String mes, String type) {
		if ("pankinglist".equals(type)) {
			this.accessPankingList(mes);
		}
	}

	public void accessPankingList(String mes) {
		// 获取排行榜面板
		RankingListJpanel rankingListJpanel = RankingListJframe.getRankingListJframe().getRankingListJpanel();
		// 将mes转换为UserRoleArrBean对象
		UserRoleArrBean listRoleMes = GsonUtil.getGsonUtil().getgson().fromJson(mes, UserRoleArrBean.class);
		// 如果UserRoleArrBean对象不为空，且list不为空，且list的大小不为0
		if (listRoleMes != null && listRoleMes.getList() != null && listRoleMes.getList().size() != 0) {
			// 获取index
			int index = listRoleMes.getIndex();
			// 如果index为8
			try {
				// 延迟1秒
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            if (index == 6 || index == 8) {
				// 获取天梯面板
				LadderJpanel ladderJpanel = LadderJframe.getLadderJframe().getLadderJpanel();
				// 将list设置给天梯面板
				ladderJpanel.setLoginResultList(listRoleMes.getList());
				// 将currSeason设置给天梯面板
				ladderJpanel.setCurrSeason(listRoleMes.getCurrSeason());
				// 将seasonInfo设置给天梯面板
				ladderJpanel.setSeasonInfo(listRoleMes.getSeasonInfo());
			} else {
				//清空rankingListJpanel的VerVectors
				rankingListJpanel.getVerVectors().clear();
				//重新设置rankingListJpanel的parts
				rankingListJpanel.parts = new NewPart[5];
				//重新设置ranking
				int ranking = 0;

				//遍历listRoleMes中的元素
				for(int i = 0; i < listRoleMes.getList().size(); ++i) {
					//如果i小于5
					if (i < 5) {
						//如果((LoginResult)listRoleMes.getList().get(i)).getSkin()不为空
						if (StringUtils.isNotBlank(listRoleMes.getList().get(i).getSkin())) {
							//调用changeskin方法，改变skin
							rankingListJpanel.parts[i] = this.changeskin(listRoleMes.getList().get(i));
						} else {
							//调用SpriteFactory的createPart方法，创建parts
							rankingListJpanel.parts[i] = SpriteFactory.createPart(listRoleMes.getList().get(i).getSpecies_id().longValue(), 2, 1, null);
						}
					}

					if (listRoleMes.getList().get(i).getPkrecord() != null) {
						BigDecimal pkrecord = listRoleMes.getList().get(i).getPkrecord();
						rankingListJpanel.getPk(pkrecord);
					}

					rankingListJpanel.getVerVectors().addElement(listRoleMes.getList().get(i));
					if (listRoleMes.getList().get(i).getRolename().equals(ImageMixDeal.userimg.getRoleShow().getRolename())) {
						ranking = i + 1;
					} else {
						rankingListJpanel.getLabtext4().setText(i + 1 + "");
					}
				}

				if (ranking != 0) {
					rankingListJpanel.getLabtext4().setText(ranking + "");
				} else {
					rankingListJpanel.getLabtext4().setText("未上榜");
				}
				if (!RankingListJframe.getRankingListJframe().isVisible()) {
					FormsManagement.showForm(60);
				}

			}
		}
	}

	public NewPart changeskin(LoginResult l) {
		String skin = l.getSkin();
		NewPart part = null;
		String[] vs = skin.split("\\|");

		for(int i = 0; i < vs.length; ++i) {
			NewPart newPart;
			if (!vs[i].startsWith("X") && !vs[i].startsWith("P")) {
				if (vs[i].startsWith("S")) {
					skin = vs[i].substring(1);
				} else if (vs[i].startsWith("C")) {
					newPart = SpriteFactory.createPart("tx/" + vs[i].substring(1), -2, 6, null);
					if (part == null) {
						part = newPart;
					} else {
						part = part.addPart(newPart);
					}
				} else if (vs[i].startsWith("B") && !ImageDressJframe.getImageDressJframe().getDressJpanel().getJCheckBox().isSelected()) {
					String cb = vs[i].substring(1);
					newPart = SpriteFactory.createPart("tx/" + cb + "0", -2, -5, null);
					if (part == null) {
						part = newPart;
					} else {
						part = part.addPart(newPart);
					}

					newPart = SpriteFactory.createPart("tx/" + cb + "1", -2, 5, null);
					part = part.addPart(newPart);
				}
			} else {
				String[] ts = vs[i].substring(1).split("_");
				newPart = SpriteFactory.createPart("tx/tx" + ts[0], -2, Integer.parseInt(ts[1]), null);
				if (part == null) {
					part = newPart;
				} else {
					part = part.addPart(newPart);
				}
			}
		}

		NewPart newPart;
		if (skin != null) {
			newPart = SpriteFactory.createPart(skin, 2, 1, null);
			if (part == null) {
				part = newPart;
			} else {
				part = part.addPart(newPart);
			}
		} else {
			newPart = SpriteFactory.createPart(l.getSpecies_id().longValue(), 2, 1, null);
			if (part == null) {
				part = newPart;
			} else {
				part = part.addPart(newPart);
			}
		}

		return part;
	}
}
